package com.shop.controller;

import com.shop.pojo.User;
import com.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login")
    public String login(){
        return "/login";
    }
    @RequestMapping(value = "/doLogin")
    public String doLogin(String username,
                          String userPassword,
                          String valideCode,
                          HttpSession session,
                          Model model){
        String Code = (String) session.getAttribute("vrifyCode");
        if(!valideCode.equals(Code)){
            //错误提示
            model.addAttribute("errorMsg","验证码错误");
            //返回登录页面
            return "login";
        }
        List<User> list = this.userService.loginUser(username, userPassword);
        if (list!=null&&list.size()>0){
            //获取登陆的用户信息
            User user = list.get(0);
            //将用户信息存在session中
            session.setAttribute("user",user);
            return "redirect:/findAuctionList";
        }else {
            model.addAttribute("errorMsg","账号或密码错误");
            return "login";
        }
    }
}
